// WhatIsMyIP.cpp : Defines the entry point for the application.
//

#include "stdafx.h"
#include "WhatIsMyIP.h"

#include <winsock2.h>
#include <ws2tcpip.h>

INT_PTR CALLBACK WhatIsMyIPDialog(HWND hDlg, UINT message, WPARAM wParam, LPARAM lParam)
{
    switch (message)
    {
        case WM_INITDIALOG:
            {
                // Create a Done button and size it.  
                SHINITDLGINFO shidi;
				SHMENUBARINFO mbi;

                shidi.dwMask = SHIDIM_FLAGS;
                shidi.dwFlags = SHIDIF_DONEBUTTON | SHIDIF_SIPDOWN | SHIDIF_SIZEDLGFULLSCREEN;
                shidi.hDlg = hDlg;
                SHInitDialog(&shidi);

				// Create a menu bar
                memset(&mbi, 0, sizeof(SHMENUBARINFO));
				mbi.cbSize     = sizeof(SHMENUBARINFO);
				mbi.hwndParent = hDlg;
				mbi.nToolBarId = IDR_MENU;
				mbi.hInstRes   = GetModuleHandle(NULL);
				mbi.dwFlags = SHCMBF_HMENU;
	            SHCreateMenuBar(&mbi);

				// Fake an IDC_REFRESH command so we can update
				// the IP address information immediatly.
				PostMessage(hDlg, WM_COMMAND, IDC_REFRESH, 0);
            }
            return (INT_PTR)TRUE;

        case WM_COMMAND:
            switch (LOWORD(wParam))
			{
				case IDOK:
					EndDialog(hDlg, LOWORD(wParam));
					return (INT_PTR)TRUE;

				case IDC_REFRESH:
					{
						WCHAR szTemp[128];
						char szHostName[128];

						// Get the hostname of the device. Default to localhost
						// if we get an error
						if (gethostname(szHostName, sizeof(szHostName)) != 0)
							strcpy(szHostName, "localhost");
						mbstowcs(szTemp, szHostName, sizeof(szHostName));
						SetDlgItemText(hDlg, IDC_EDIT_HOSTNAME, szTemp);

						// Get the list of IP addresses associated with this device
						SendDlgItemMessage(hDlg, IDC_COMBO_IPADDRESSES, CB_RESETCONTENT, 0, 0);
						struct addrinfo *res = NULL;
						struct addrinfo hints;

						memset (&hints, 0, sizeof (hints));
						hints.ai_family = AF_UNSPEC;
						hints.ai_socktype = SOCK_STREAM;
						hints.ai_flags |= AI_PASSIVE;

						if ((getaddrinfo(szHostName, NULL, &hints, &res)) == 0)
						{
							// For each IP address convert it into a textual form
							// and add it to the combo box.
							struct addrinfo *current = res;
							while (current)
							{
								if (getnameinfo(current->ai_addr, current->ai_addrlen, szHostName, sizeof(szHostName), NULL, 0, NI_NUMERICHOST | NI_NUMERICSERV) == 0)
								{
									mbstowcs(szTemp, szHostName, sizeof(szTemp) / sizeof(WCHAR));
									SendDlgItemMessage(hDlg, IDC_COMBO_IPADDRESSES, CB_ADDSTRING, 0, (LPARAM)szTemp);
								}

								current = current->ai_next;
							}

							freeaddrinfo(res);
						}

						// Select the first IP address in the combo box
						SendDlgItemMessage(hDlg, IDC_COMBO_IPADDRESSES, CB_SETCURSEL, 0, 0);
					}
					return (INT_PTR)TRUE;
			}
            break;

        case WM_CLOSE:
            EndDialog(hDlg, message);
            return (INT_PTR)TRUE;

    }
    return (INT_PTR)FALSE;
}

int WINAPI WinMain(HINSTANCE hInstance,
                   HINSTANCE hPrevInstance,
                   LPTSTR    lpCmdLine,
                   int       nCmdShow)
{
	WSADATA wsaData;
	WSAStartup(WINSOCK_VERSION, &wsaData);

	SHInitExtraControls();

	DialogBox(hInstance, (LPCTSTR)IDD_WHATISMYIP, NULL, WhatIsMyIPDialog);

	WSACleanup();

	return 0;
}